/***********************************************************
 * Product Name: ExcelQuicker
 * Copyright (c)2005, Eunge
 * Author: Eunge
 * Web Site: www.eudev.net
 * Mail/MSN: eudev.net@yeah.net
 * ********************************************************/
using System;
using System.Collections;

namespace ExcelQuicker.Framework
{
	/// <summary>
	/// The header class of tables in ExcelQuicker. It stands for a header row at the top of output table
	/// </summary>
	public class EQTableHeader: EQRangeBase  	
	{
		private ArrayList mRangeCollection = new ArrayList();
		private ArrayList mRangeOccupyCellsCollection = new ArrayList();

		/// <summary>
		/// Get the amout of cells. It must be equal to the amout of columns of destination table
		/// </summary>
		public int CellCount
		{
			get
			{
				int intCount = 0;
				IEnumerator objIEnumerator = this.mRangeOccupyCellsCollection.GetEnumerator();
				while(objIEnumerator.MoveNext())
				{
					intCount += int.Parse(objIEnumerator.Current.ToString(),EQKit.ObjNumberFormatInfo);
				}
				return intCount;
			}
		}

		/// <summary>
		/// Add a string array as a part of current header with setting a style collection
		/// </summary>
		/// <param name="text"></param>
		/// <param name="objEQStyleBaseCollection">Style object</param>
		/// <example>
		/// <br>EQTableHeader objEQTableHeader = new EQTableHeader();</br>
		/// <br>string[] strHeader0 = {"Name", "Age", "Career"};</br>
		/// <br>string[] strHeader1 = {"Position", "Salary"};</br>
		/// <br>//Assume that we have defined style as a EQStyleBaseCollection class object</br>
		/// <br>objEQTableHeader.Add(strHeader0, style);  //So, now the header contains three columns, Name, Age and Career.</br>
		/// <br>objEQTableHeader.Add(strHeader1, style);  //In total, there are five columns included in the header, Name, Age, Career, Position and Salary</br>
		/// </example>
		public void Add(string[] text, EQStyleBaseCollection objEQStyleBaseCollection)
		{
			object[,] objArray = new object[1,text.Length];
			for(int i=0; i<text.Length; i++)
				objArray[0,i] = text[i];
			EQRange objEQRange = new EQRange(objArray);

			if(objEQStyleBaseCollection != null)
			{
				IEnumerator objIEnumerator = objEQStyleBaseCollection.GetEnumerator();
				while(objIEnumerator.MoveNext())
				{
					objEQRange.Style.Add(objIEnumerator.Current as EQStyleBase);
				}
			}

			this.mRangeCollection.Add(objEQRange);
			this.mRangeOccupyCellsCollection.Add(text.Length);
		}

		/// <summary>
		/// Add a string array as a part of current header
		/// </summary>
		/// <param name="text"></param>
		/// <example>
		/// <br>EQTableHeader objEQTableHeader = new EQTableHeader();</br>
		/// <br>string[] strHeader0 = {"Name", "Age", "Career"};</br>
		/// <br>string[] strHeader1 = {"Position", "Salary"};</br>
		/// <br>objEQTableHeader.Add(strHeader0);  //So, now the header contains three columns, Name, Age and Career.</br>
		/// <br>objEQTableHeader.Add(strHeader1);  //In total, there are five columns included in the header, Name, Age, Career, Position and Salary</br>
		/// </example>
		public void Add(string[] text)
		{
			this.Add(text, null);
		}

		/// <summary>
		/// Add a string as a part of current header with setting a style collection
		/// </summary>
		/// <param name="text"></param>
		/// <param name="mergeCellCount">Occupied amount of columns</param>
		/// <param name="objEQStyleBaseCollection">Style collection</param>
		/// <example>
		/// <br>
		/// //If you want to add two columns header to a table with five columns. This header assumes as a total header of other below sub header.
		/// In this case, you can call .Add() function twice and specialize how to merge columns. You can see this case in Report 4 of winform sample.
		/// </br>
		/// <br>objEQTableHeader.Add("Product Information", 2, objEQStyleBaseCollection);</br>
		/// <br>objEQTableHeader.Add("Quantity And Sales", 3, objEQStyleBaseCollection);</br>
		/// <br>Before above call, we assume that there has been a header already with five columns. Therefore, the structure of headers now is as follows.</br>
		/// <br>Product Information    |     Quantity And Sales</br>
		/// <br>      Col0   |   Col1      |    Col2  |  Col3  |  Col4</br>
		/// </example>
		public void Add(string text, int mergeCellCount, EQStyleBaseCollection objEQStyleBaseCollection)
		{
			object[,] objArray = new object[1,1];
			objArray[0,0] = text;
			EQRange objEQRange = new EQRange(objArray);
			objEQRange.MergeCells = true;

			if(objEQStyleBaseCollection != null)
			{
				IEnumerator objIEnumerator = objEQStyleBaseCollection.GetEnumerator();
				while(objIEnumerator.MoveNext())
				{
					objEQRange.Style.Add(objIEnumerator.Current as EQStyleBase);
				}
			}

			this.mRangeCollection.Add(objEQRange);
			this.mRangeOccupyCellsCollection.Add(mergeCellCount);
		}

		/// <summary>
		/// Add a string as a part of current header
		/// </summary>
		/// <param name="text"></param>
		/// <param name="mergeCellCount">Occupied amount of columns</param>
		/// <example>
		/// <br>
		/// //If you want to add two columns header to a table with five columns. This header assumes as a total header of other below sub header.
		/// In this case, you can call .Add() function twice and specialize how to merge columns. You can see this case in Report 4 of winform sample.
		/// </br>
		/// <br>objEQTableHeader.Add("Product Information", 2);</br>
		/// <br>objEQTableHeader.Add("Quantity And Sales", 3);</br>
		/// <br>Before above call, we assume that there has been a header already with five columns. Therefore, the structure of headers now is as follows.</br>
		/// <br>Product Information    |     Quantity And Sales</br>
		/// <br>      Col0   |   Col1      |    Col2  |  Col3  |  Col4</br>
		/// </example>
		public void Add(string text, int mergeCellCount)
		{
			this.Add(text, mergeCellCount, null);
		}
		

		/// <summary>
		/// Set the position of these headers. This function is internal called usually by EQTable object when binding this header to a table
		/// </summary>
		/// <param name="top"></param>
		/// <param name="left"></param>
		protected internal void Prepare(int top, int left)
		{
			int intCount = 0;
			int i = 0;
			IEnumerator objIEnumerator = this.mRangeCollection.GetEnumerator();
			while(objIEnumerator.MoveNext())
			{
				EQRange objEQRange = objIEnumerator.Current as EQRange;
				objEQRange.SetTop(top);
				objEQRange.SetLeft(left + intCount);
				objEQRange.Right = objEQRange.Left + int.Parse(this.mRangeOccupyCellsCollection[i++].ToString(), EQKit.ObjNumberFormatInfo) - 1;
				intCount += objEQRange.ColumnCount;
			}
		}

		/// <summary>
		/// Output the header to excel document
		/// </summary>
		/// <param name="objWorksheet"></param>
		protected internal override void Output(Excel.Worksheet objWorksheet)
		{
			this.DoBeforeOperationEvent(objWorksheet);
			
			IEnumerator objIEnumerator = this.mRangeCollection.GetEnumerator();
			while(objIEnumerator.MoveNext())
			{
				(objIEnumerator.Current as EQRange).Output(objWorksheet);
			}

			this.DoAfterOperationEvent(objWorksheet);
		}
	}
}
